#!/usr/bin/perl
# map_2_bmp.pl
#
# April 2010, James Redus 
#
# This script is released under the terms of the MIT License:
# --------------------------------------------------------------
## Permission is hereby granted, free of charge, to any person
## obtaining a copy of this software and associated documentation
## files (the "Software"), to deal in the Software without
## restriction, including without limitation the rights to use,
## copy, modify, merge, publish, distribute, sublicense, and/or sell
## copies of the Software, and to permit persons to whom the
## Software is furnished to do so, subject to the following
## conditions:
##
## The above copyright notice and this permission notice shall be
## included in all copies or substantial portions of the Software.
##
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
## EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
## OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
## NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
## HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
## WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
## FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
## OTHER DEALINGS IN THE SOFTWARE.
# --------------------------------------------------------------
# 4/11/2010,jeredus - Added project to Google code (http://code.google.com/p/djmaputils/)
# 
#
# This script creates bitmap images from Dynojet power commander files.
#
###########################################################
use File::Find;

require "getopts.pl";
require 'dumpvar.pl';
require 'pwr_cmdr.pl';
require 'pwr_cmdr_bmp.pl';

my $Width=300;
my $Height=500;
my @Mapfiles=();
my $Color_peak=25;


&Getopts('ivhd:f:s:p:');
my $Usage="Experimental script to create bitmap files from Power Commander 3/5 files.
Usage: $0 [-v] [-h] [-i] [-p <peak>] -d <dir>
    or
       $0 [-v] [-h] [-i] [-p <peak>] -f <mapfile>

where
    -v ....................... verbose output
    -i ....................... interpolate the bitmap 
    -p <peak> ................ the 'Color Peak' - cell value that gets max color (default:$Color_peak)
    -d <dir> ................. open a whole dir containing map files (.djm or .pvm)
    -f <mapfile> ............. open a single map file (.djm or .pvm)
    -s <WxH> ................. width and height of bitmap (default: ${Width}x$Height)
    -h ....................... this Help

You have to use either -f or -d options.

Examples:
    $0 -f c:\tmp\mymap.pvm

    $0 -d c:\tmp\some_maps\
";

if ($opt_h) { print "$Usage\n"; exit(0); }

if (! ($opt_d || $opt_f) ) { 
    print "You forgot to use either -f or -d option.\n$Usage\n"; 
    exit(0); 
}

if ($opt_s) {
    if($opt_s =~ m/(\d+)\D+(\d+)/) {
        $Width=$1;
        $Height=$2;
    }
}

if ($opt_p) {
    if($opt_p =~ m/^\d+$/) {
        $Color_peak=$opt_p;
    }else{
        print "$Usage\nInvalid Color Peak ($opt_p).\n";
        exit(0);
    }
}

select(STDOUT); $|=1;

###########################################################
###########################################################
#
# This sub is used by the find() function. It stores anything
# that looks like a PC map file in an array.
#
sub store_map_name() {

    # print "File name is $_\n\t\tFull path is $File::Find::name\n";
    if ($File::Find::name =~ m/(djm|pvm)$/i) {
        push (@Mapfiles,$File::Find::name);
    }
}

###########################################################
###########################################################

if ($opt_d) {
    if (! -d $opt_d ) {
        print "Invalid directory ($opt_d). $Usage\n";
        exit(1);
    }
    find(\&store_map_name, "$opt_d");

}elsif ($opt_f) {
    if (! -f $opt_f ) {
        print "Invalid file ($opt_f). $Usage\n";
        exit(1);
    }
    push(@Mapfiles,$opt_f)

} else {
    print "You forgot to use either -f or -d option.\n$Usage\n"; 
    exit(0); 
}

print "Bitmaps will be $Width x $Height.\n";
$time0=time;
foreach $mapfile (@Mapfiles) {
    my $bmp_name;
    # print "Reading $mapfile\n";
    if ($mapfile =~ m/djm/i) {
        %h=read_djm_file($mapfile,$opt_v);
    }elsif($mapfile =~ m/pvm/i) {
        %h=read_pvm_file($mapfile,$opt_v);
    }else{
        next;
    }

    if($opt_i) {
        $bmp_name=$mapfile . ".i.bmp";
    }else{
        $bmp_name=$mapfile . ".bmp";
    }

    print "Writing $bmp_name ";
    map_2_bmp(\%h,"$bmp_name",$Width,$Height,$opt_i,$Color_peak,1); 
    print "\n";
    $time1=time;

    printf("Running time so far = %d seconds (%.3f minutes)\n",($time1-$time0),($time1-$time0)/60.0);
    

}

$time2=time;

printf("Total running time = %d seconds (%.3f minutes)\n",($time2-$time0),($time2-$time0)/60.0);
exit;



